<template>
  <div class="address-container">
    <!-- 左侧侧边栏 -->
    <div class="sidebar">
      <PersonalCenterSidebar />
    </div>

    <!-- 右侧内容区域 -->
    <div class="content-area">
      <!-- 表格区域 -->
      <div class="table-container">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="orderId" label="订单号" />
          <el-table-column prop="customerId" label="商家" />
          <el-table-column prop="orderDate" label="下单日期" />
          <el-table-column prop="totalAmount" label="金额" />
          <el-table-column prop="status" label="状态" />
          <el-table-column prop="shippingAddress" label="地址" />
          <el-table-column prop="notes" label="备注" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="text" @click="handleEdit(scope.row)">商品状态</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 按钮容器 -->
      <div class="button-container">

        <el-button type="primary" @click="goBack">返回</el-button>
      </div>
    </div>

    <!-- 点击新增，弹出的对话框 -->
    <el-dialog
        v-model="dialogVisible"
        title="提示"
        width="30%"
    >
      <el-form :model="form" label-width="120px">
        <el-form-item label="状态">
          <el-input v-model="form.status" style="width: 80%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="danger" @click="save(true)">已收货</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import router from "@/router"; // 引入路由实例
import PersonalCenterSidebar from "@/views/PersonalCenterSidebar.vue";

export default {
  name: 'Home',
  components: {PersonalCenterSidebar},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      pageSize: 5,
      tableData: [],
      userNameQuery: ''
    }
  },
  methods: {
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleSizeChange() {
      this.load();
    },
    handleCurrentChange() {
      this.load();
    },
    add() {
      this.dialogVisible = true;
      this.form = {};
    },
    save(isReceived) {
      if (isReceived) {
        this.form.status = '已收货';
      }

      if (this.form.orderId) {
        request.put("/user/updateOrderStatusByUser", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load();
          this.dialogVisible = false;
        })
      } else {
        request.post("/user/addUser", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load();
          this.dialogVisible = false;
        })
      }
    },
    load() {
      request.get("/user/getOrderPageStatus", {
        params: {
          userId: 1,
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      })
    },
    searchByRoomName() {
      request.get("/user/getUseByName", {
        params: {
          username: this.userNameQuery
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    goBack() {
      router.go(-1);
    }
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
/* 容器样式 */
.address-container {
  display: flex;
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 1000px;
  margin: 0 auto;
}

/* 左侧侧边栏样式 */
.sidebar {
  width: 250px;
  margin-right: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 右侧内容区域样式 */
.content-area {
  flex-grow: 1;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 表格容器样式 */
.table-container {
  margin-bottom: 20px;
}

/* 按钮容器样式 */
.button-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 按钮样式 */
.el-button {
  margin: 0 5px;
  transition: all 0.3s;
}

.el-button:hover {
  transform: scale(1.05);
}

/* 输入框样式 */
.el-input {
  transition: border-color 0.3s;
}

.el-input:focus-within {
  border-color: #409eff;
}

/* 对话框样式 */
.el-dialog {
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-dialog__header {
  background-color: #f0f0f0;
  padding: 15px;
  border-bottom: 1px solid #ddd;
  border-radius: 10px 10px 0 0;
}

.el-dialog__body {
  padding: 20px;
}

.el-dialog__footer {
  padding: 10px 20px;
  border-top: 1px solid #ddd;
  border-radius: 0 0 10px 10px;
  text-align: right;
}
</style>